package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AddressDao;
import com.vf.admin.client.service.AppSsCitiesService;
import com.vf.admin.client.service.AppSsProvincesService;
import com.vf.admin.client.service.SsAreaService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.Address;
import com.vf.admin.miaodong.entity.SsArea;
import com.vf.admin.miaodong.entity.SsCities;
import com.vf.admin.miaodong.entity.SsProvinces;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl {

    @Autowired
    private AddressDao addressDao;
    @Autowired
    private AppSsProvincesService appSsProvincesService;
    @Autowired
    private AppSsCitiesService appSsCitiesService;
    @Autowired
    private SsAreaService areaService;


    /**
     * banner新增
     *
     * @param param 根据需要进行传值
     * @return
     */
    public void saveOrUpdate(Address param) {
        if (param.getId() != null && param.getId() > 0) {
            param.setCreateTime(new Date());
            addressDao.update(param);
        } else {
            //修改
            if (param.getStatus() != null && param.getStatus() == 1) {
                List<Address> list = listByUserId(param.getUserId());
                for (Address a : list) {
                    a.setStatus(2);
                    addressDao.update(a);
                }
            }
            addressDao.add(param);
        }
    }

    public List<Address> page(Map<String, Object> paraMap, SplitPageBean page) {

        return addressDao.page(paraMap, page);
    }

    public List<Address> list(Map<String, Object> paraMap) {
        List<Address> list = addressDao.list(paraMap);

        for (Address a : list) {
            SsProvinces address1 = appSsProvincesService.get(a.getProvinceId());
            SsCities address2 = appSsCitiesService.getCitie(a.getCityId());
            SsArea address3 = areaService.get(a.getTownId());
            a.setProvince(address1);
            a.setCity(address2);
            a.setTown(address3);
        }
        return list;
    }

    public List<Address> listByUserId(Integer userId) {

        List<Address> list = addressDao.listByUserId(userId);

        for (Address a : list) {
            SsProvinces address1 = appSsProvincesService.get(a.getProvinceId());
            SsCities address2 = appSsCitiesService.getCitie(a.getCityId());
            SsArea address3 = areaService.get(a.getTownId());
            a.setProvince(address1);
            a.setCity(address2);
            a.setTown(address3);
        }
        return list;
    }

    public Address info(Integer id) {
        Address address = addressDao.get(id, Address.class);

        SsProvinces address1 = appSsProvincesService.get(address.getProvinceId());
        SsCities address2 = appSsCitiesService.getCitie(address.getCityId());
        SsArea address3 = areaService.get(address.getTownId());
        address.setProvince(address1);
        address.setCity(address2);
        address.setTown(address3);
        return address;
    }

    public void remove(String id) {
        addressDao.del("delete from address where id = "+ id, new ArrayList<>());
    }

}
